package com.example.lenovo.a203_yhy_ordering_3.fragments;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.lenovo.a203_yhy_ordering_3.R;
import com.example.lenovo.a203_yhy_ordering_3.activity.FoodInfoActivity;
import com.example.lenovo.a203_yhy_ordering_3.activity.FoodListActivity;
import com.example.lenovo.a203_yhy_ordering_3.adapter.CollectFoodAdapter;
import com.example.lenovo.a203_yhy_ordering_3.adapter.CollectShopAdapter;
import com.example.lenovo.a203_yhy_ordering_3.beans.AllUserCollectBean;
import com.example.lenovo.a203_yhy_ordering_3.listener.AllUserCollectListener;
import com.example.lenovo.a203_yhy_ordering_3.model.AllUserCollectModel;

import java.util.List;

public class CollectShopFragment extends Fragment {
    private RecyclerView recyclerView;
    private List<AllUserCollectBean> allUserCollectBeans;
    private CollectShopAdapter collectShopAdapter;
    private int user_id;
    private SharedPreferences sharedPreferences;

    private int page=1; // 代表页数，并初始化为1，代表第1页。
    private int lastVisibleItemPosition;//最后一条可见条目的位置

    public CollectShopFragment() {

    }

    private AllUserCollectListener allUserCollectListener = new AllUserCollectListener() {
        @Override
        public void onResponse(List<AllUserCollectBean> collectBean) {
            if(page==1)
                allUserCollectBeans = collectBean;
            else{
                allUserCollectBeans.removeAll(collectBean);
                allUserCollectBeans.addAll(collectBean);
            }
            collectShopAdapter.setList(allUserCollectBeans);
        }

        @Override
        public void onFail(String mag) {
            Toast.makeText(getContext(),mag,Toast.LENGTH_SHORT).show();
        }
    };

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_collect_shop_fragment,container,false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        sharedPreferences=getActivity().getSharedPreferences("user",Context.MODE_PRIVATE);
        user_id=Integer.parseInt(sharedPreferences.getString("userid","获取userid失败"));

        initRecyclerview(view);
        AllUserCollectModel allUserCollectModel=new AllUserCollectModel();
        allUserCollectModel.getAllUserCollectResult(user_id,0,allUserCollectListener);

        click();
    }

    public void initRecyclerview(View view){
        recyclerView=view.findViewById(R.id.recyclerview);
        final LinearLayoutManager linearLayoutManager=new LinearLayoutManager(getContext());
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setHasFixedSize(true);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.addItemDecoration(new DividerItemDecoration(getContext(),DividerItemDecoration.VERTICAL));
        collectShopAdapter=new CollectShopAdapter();
        recyclerView.setAdapter(collectShopAdapter);
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItemPosition + 1 == allUserCollectBeans.size())    {
//                    page++;
                    //再次实例化Model，调用方法获取网络数据，请求新一页数据
                    AllUserCollectModel allUserCollectModel=new AllUserCollectModel();
                    allUserCollectModel.getAllUserCollectResult(user_id,0,allUserCollectListener);
                }
            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                lastVisibleItemPosition = linearLayoutManager.findLastVisibleItemPosition();//滚动结束后将赋值为可见条目中最后一条位置
            }
        });
    }

    public void click(){
        collectShopAdapter.setOnItemClickListener(new CollectShopAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent=new Intent(getContext(),FoodListActivity.class);
                intent.putExtra("shop_id",allUserCollectBeans.get(position).getShop_id());
                intent.putExtra("shopname",allUserCollectBeans.get(position).getShopname());
                startActivity(intent);
            }
        });
    }

}
